package itsm.isperp.module.controller.task;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.utils.TreeNodeUtils;
import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.task.TskInspectTemplate;
import itsm.isperp.module.service.task.TskInspectTemplateService;

import me.chanjar.weixin.common.util.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2015-11-05 12:28
 */
@Controller
@RequestMapping("task/inspectTemplate")
public class TskInspectTemplateController extends
		TreeNodeController<TskInspectTemplate> {

	@Autowired
	protected TskInspectTemplateService tskInspectTemplateService;

	@Override
	public TskInspectTemplateService getService() {
		return this.tskInspectTemplateService;
	}

	@Override
	public String getPageTitle() {
		return "巡检模板 ";
	}

	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req) {

		ModelAndView mv = super.edit(id, req);

		TskInspectTemplate model = (TskInspectTemplate) mv.getModelMap().get(
				"model");

		String defId = req.getParameter("defId");
		if (StringUtils.isNotEmpty(defId)) {
			model.setDefId(defId);
		}

		return mv;
	}

	/**
	 * 取得树形结构的的json数据
	 * 
	 * @return 菜单的json数据
	 */
	@RequestMapping("getTree")
	@ResponseBody
	@Override
	public List<TskInspectTemplate> getTree(String id, boolean asyncLoad,
			String parentId, boolean showRoot, HttpServletRequest req) {

		DataRequest request = new JqGridRequest().isPageable(false).setSort(
				"order_num", Direction.ASC);

		request.addFilterRule("def_id", JqGridFilterRuleOp.eq,
				req.getParameter("defId"));

		List<TskInspectTemplate> tree = this.getService().findAll(request)
				.getContent();

		return TreeNodeUtils.parseAndAddRootFormList(TskInspectTemplate.class,
				tree, true);
	}

	/**
	 * 模板项编辑
	 * 
	 * @param id
	 *            模板id
	 * @return 模板项编辑界面
	 */
	@RequestMapping("treeEdit")
	public ModelAndView treeEdit(String id) {
		ModelAndView mv = this.getModelAndView();
		mv.addObject("defId", id);
		return mv;
	}

	/**
	 * 模板管理
	 * 
	 * @return
	 */
	@RequestMapping("preview")
	public ModelAndView preview(String defId) {
		ModelAndView mv = this.getModelAndView();
		mv.addObject("defId", defId);
		return mv;
	}
}
